package cn.itcast.hotel.listener;

import cn.itcast.hotel.pojo.Hotel;
import cn.itcast.hotel.pojo.HotelDoc;
import com.alibaba.fastjson.JSON;
import org.elasticsearch.action.delete.DeleteRequest;
import org.elasticsearch.action.get.GetRequest;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.action.update.UpdateRequest;
import org.elasticsearch.action.update.UpdateRequestBuilder;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.common.xcontent.XContentType;
import org.elasticsearch.index.query.QueryBuilders;
import org.springframework.amqp.core.ExchangeTypes;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.xml.ws.BindingType;
import java.io.IOException;

/**
 * @Author WSJ
 * @Date 2022-03-06-15:50
 * @Version 1.0 （版本号）
 */

@Service
public class HotelListener {
    @Autowired
    private RestHighLevelClient client;


    /**
     * 消费者接收修改或新增消息，将结果同步到elasticsearch
     * @param massage
     * @throws IOException
     */
    @RabbitListener(bindings =@QueueBinding(
            value = @Queue("hotel.insert.queue"),
            exchange = @Exchange(name = "hotel.exchange",type = ExchangeTypes.DIRECT),
            key = {"hotel.insert"}
    ))
    public void insert(String massage) throws IOException {
        Hotel hotel = JSON.parseObject(massage, Hotel.class);
        HotelDoc hotelDoc=new HotelDoc(hotel);
            GetRequest exiRequest=new GetRequest("hotel",hotel.getId().toString());
            boolean exists = client.exists(exiRequest, RequestOptions.DEFAULT);
            if (exists) {//当前id存在则修改
                UpdateRequest request= new UpdateRequest("hotel",hotel.getId().toString());
                request.doc(JSON.toJSONString(hotelDoc), XContentType.JSON);
                client.update(request, RequestOptions.DEFAULT);
            }else {//当前id不存在则新增
                IndexRequest request=new IndexRequest("hotel");
                request.id(hotel.getId().toString());
                request.source(JSON.toJSONString(hotelDoc), XContentType.JSON);
                client.index(request,RequestOptions.DEFAULT);
            }

    }


    /**
     * 消费者接收删除消息，将结果同步到elasticsearch
     * @param massage
     * @throws IOException
     */
    @RabbitListener(bindings = @QueueBinding(
            value = @Queue("hotel.delete.queue"),
            exchange = @Exchange(name = "hotel.exchange",type = ExchangeTypes.DIRECT),
            key = {"hotel.delete"}
    ))
    public void delete(Long massage) throws IOException {
        DeleteRequest request=new DeleteRequest("hotel",massage.toString());
        client.delete(request,RequestOptions.DEFAULT);
    }
}
